import org.omg.CORBA.IRObject;

import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 23735
 * Date: 2023-06-27
 * Time: 20:53
 */
public class Solution {

    /**
     *  使用栈
     * @param s
     * @return
     */
    public String removeStars(String s) {
        char[] arr = s.toCharArray();
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == '*') {
                stack.pop();
            } else {
                stack.push(arr[i]);
            }
        }
        StringBuilder stringBuilder = new StringBuilder();
        while (!stack.isEmpty()) {
            stringBuilder.append(stack.pop());
        }
        return stringBuilder.reverse().toString();
    }

    /**
     *  双指针原地操作
     * @param s
     * @return
     */
    public String removeStars2(String s) {
        char[] arr = s.toCharArray();
        int left = 0;
        int right = 0;
        for (; right < arr.length; right++) {
            if (arr[right] == '*') {
                left--;
            } else {
                arr[left] = arr[right];
                left++;
            }
        }
        return String.valueOf(arr).substring(0, left);
    }
}
